﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using PowersourceUIToolkit;
using UCL.Powersource;

namespace ToolkitTestApp
{
    public class TestAppBootStrapper : Bootstrapper
    {

        public const string KEY_Bootstrapper = "bootstrapper";
        protected override void Configure()
        {
            Debug.WriteLine("[{0}] : Configure", this);
            var catalog = new AggregateCatalog(
                    AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                    );

            container = new CompositionContainer(catalog);


            var batch = new CompositionBatch();

            batch.AddExportedValue<IUIManager>(new UIManager());
            //batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue<ILog>(LogManager.GetLog(null));
            batch.AddExportedValue(container);
            batch.AddExportedValue(catalog);
            container.Compose(batch);
        }
        
        private CompositionContainer container;

        

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = container.GetExportedValues<object>(contract);

            if (exports.Count() > 0)
                return exports.First();

            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }

        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }


        /// <summary>
        /// Current only load Powersource assemblyFile 
        /// </summary>
        /// <returns>A list of assemblies to inspect.</returns>
        protected override IEnumerable<Assembly> SelectAssemblies()
        {
            //var appDomain = AppDomain.CurrentDomain;
            //var assemblies = appDomain.GetType().GetMethod("GetAssemblies")
            //                     .Invoke(appDomain, null) as Assembly[] ?? new Assembly[] { };
            //var availableApplications = 

            //    //assemblies
            //    //        .Where(x => x.EntryPoint != null && x.GetTypes().Any(t => t.IsSubclassOf(typeof(Application))));

            //return new[] { availableApplications.FirstOrDefault() };
            return new[] { Assembly.GetExecutingAssembly() };
        }


        protected override void InitializeComposableParts()
        {
            return;
        }

        protected override bool ValidateAssembly(string assemblyFilename)
        {
            return true;
        }
    }
}
